package com.hmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class  OrderRabbitConfig {


    /**
     * 创建下单交换机
     */
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange("order.exchange",true,false);
    }


    /**
     * 创建延迟队列
     */
    @Bean
    public Queue delayQueue(){
        return QueueBuilder
                .durable("order.queue")
                .ttl(10000) //过期时间，单位毫秒
                .deadLetterExchange("dl.exchange")//指定死信交换机
                .deadLetterRoutingKey("dl.key")//指定死信路由key
                .build();
    }

    /**
     * 绑定死信交换机和死信队列
     */
    @Bean
    public Binding bindDelayQueueToExchange(DirectExchange orderExchange,Queue delayQueue){
        return BindingBuilder.bind(delayQueue).to(orderExchange).with("order.key");
    }



    /**
     * 创建死信交换机
     */
    @Bean
    public DirectExchange createDeadLettterExchange(){
        return new DirectExchange("dl.exchange",true,false);
    }

    /**
     * 创建死信队列
     */
    @Bean
    public Queue createDeadLettterQueue(){
        return QueueBuilder
                .durable("dl.queue")
                .build();
    }

    /**
     * 绑定死信交换机和死信队列
     */
    @Bean
    public Binding bindQueueToExchange(){
        return BindingBuilder.bind(createDeadLettterQueue()).to(createDeadLettterExchange()).with("dl.key");
    }

}